class ColController < ApplicationController
  before_filter :set_cookies_or_url
  after_filter :log_filter,:except => [:index]
  layout "col"
  include ColHelper
  def index  
    redirect_to params.merge!(:action => 'show')
  end
  
  def show
    params[:cn] = params[:cn].to_i + 1
    @oper_type = 3
    @img_style = '1'    
    if params[:img_style].nil?
      @img_style = '1'     
    else
      @img_style = params[:img_style]      
    end    
    if not params[:jump_page].nil?
      if not params[:col_id].nil?  && params[:total_pages].nil?        
        if params[:jump_page].to_i == 0
          params[:jump_page] = 1
        end
        if params[:jump_page].to_i > params[:total_pages].to_i
          params[:page] = params[:total_pages]
        else
          params[:page] = params[:jump_page]
        end
        params[:id] = params[:col_id]
        params[:jump_page] = nil
      end
    end
    section = Section.find params[:id]
    @col = section
    @back_type = $back_url
    @go_page = 1
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end   
    to_page = params[:page]
    if not params[:page_for].nil?
      if not params[:page_for].empty?
        to_page = params[:page_for]
        params[:page_for] = nil
      end
    end    
    @page_for = params[:page]
    per_page = $res_per_page_word
    if @img_style == 2.to_s
      per_page = $res_per_page_img
    end
    if to_page.nil?
      to_page = 1
    end    
    host = request.domain
    le = $level_map[host.swapcase]
    @titles = section.name.to_s + request.domain
    res_l = le[:res_level]
    
    @c_st = 1
    if params[:c_st].to_i !=0
      @c_st = params[:c_st].to_i
    end
    if @c_st == 1
      @resouces = Resource.paginate :per_page => per_page, :page =>  to_page,
        :conditions =>  ["status=? and col_id=? and level <= ? ", 1, params[:id], res_l],
        :order => 'score desc'
      if @resouces.total_pages.to_i < to_page.to_i
        @resouces = Resource.paginate :per_page => per_page, :page =>  1,
          :conditions =>  ["status=? and col_id=? and level <= ? ", 1, params[:id], res_l],
          :order => 'score desc'
      end
    else
      @resouces = Resource.paginate :per_page => $res_per_page_word, :page =>  1,
        :conditions =>  ["status=? and col_id=? and level <= ? ", 1, params[:id], res_l],
        :order => 'updated_at desc',
        :limit => '15'
      if @resouces.total_pages.to_i < to_page.to_i
        @resouces = Resource.paginate :per_page => $res_per_page_word, :page =>  1,
          :conditions =>  ["status=? and col_id=? and level <= ? ", 1, params[:id], res_l],
          :order => 'score desc',
          :limit => '15'
      end
    end
    @resource = @resouces[0]
    if @resouces.size <= 1 && params[:page].nil?
      if @resource.nil?
        @resource  = Resource.find :all,
          :conditions =>  ["status=?", 1],
          :order => 'rand()',
          :limit => '1'
      end
      params[:resource] = @resource.id
      params[:go_page] = 1
      params[:id] = nil
      #针对热图推荐这种特例
      if !@resouces[0].nil?
        if params[:pp_style].to_i == 2
          redirect_to params.merge!(:action => 'show_big') and return false
        else
          redirect_to params.merge!(:action => 'show_small') and return false
        end        
      else
        @titles = section.name.to_s + request.domain
      end
    else
      @titles = section.name.to_s + request.domain
    end
    respond_to do |format|
      format.wml
    end
  end

  def img_style_to
    params[:f_i] = 1
    if "1" == params[:pp_style]      
      redirect_to params.merge!(:action => 'show_small')
    else
      redirect_to params.merge!(:action => 'show_big')
    end
  end

  def ts
    params[:cn] = params[:cn].to_i
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    @back_type = nil
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @resource = params[:resource]
    @titles = "友情提醒"
    respond_to do |format|
      format.wml
    end
  end

  def setup
    params[:cn] = params[:cn].to_i
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    @back_type = nil
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @resource = params[:resource]
    @titles = "友情提醒"
    respond_to do |format|
      format.wml
    end
  end
  


  #高清大图,版面位置比较小
  def show_big    
    params[:cn] = params[:cn].to_i + 1
    @oper_type = 4
    @resource = Resource.find(params[:resource])
    #鲜花和鸡蛋
    oper_type = params[:oper_type]
    #评论 时间间隔要大于10秒
    comment = params[:comment]
    res_score = 0.0
    if oper_type.nil? && !params[:page].nil?
      #翻页过来的
      res_score = 1.5              
    elsif oper_type.nil? && params[:page].nil?
      #栏目过来的
      res_score = 1.0
    elsif !oper_type.nil? && oper_type.to_i == 1
      #鲜花过来的
      res_score = 2.0
    end
    d_s = @resource.day_score + res_score.to_f
    w_s = @resource.week_score + res_score.to_f
    m_s = @resource.month_score + res_score.to_f
    @resource.update_attributes({:score => (@resource.score.to_f + res_score.to_f).to_f,
        :day_score => d_s,:week_score => w_s,:month_score => m_s})
    if not params[:jump_page].nil?
      if params[:jump_page].to_i == 0
        params[:jump_page] = 1
      end
      if params[:jump_page].to_i * $img_per_page > params[:total_pages].to_i
        params[:jump_page] = params[:total_pages].to_i * $img_per_page
      end
      if params[:jump_page].to_i % $img_per_page == 0
        params[:page] = (params[:jump_page].to_i / $img_per_page).to_i
      else
        params[:page] = (params[:jump_page].to_i / $img_per_page).to_i + 1
      end
      params[:id] = nil
      params[:jump_page] = nil
    end   
    
    @back_type = $back_col
    if params[:return_type].to_i == 1
      @back_type = $back_lastest
    elsif params[:return_type].to_i == 3
      @back_type = $back_chose
    elsif params[:return_type].to_i == 4
      @back_type = $back_al
    elsif params[:return_type].to_i == 9
      @back_type = $back_rank
    end
    @rt = params[:rt]
    
    section = Section.find @resource.col_id
    @col = section
    @page_for = params[:page_for]
    unless params[:page].nil?
      @go_page = params[:page]
    else
      @go_page = params[:go_page]
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 2
    end
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    host = request.domain
    le = $level_map[host.swapcase]
    res_l = le[:res_level]
    @titles = le[:name]
    res_all = Resource.find :all,
      :conditions =>  ["status=? and col_id=?  and level <= ?", 1, @resource.col_id,res_l],
      :order => 'orders asc'   
    res_all.each_with_index do |res,res_index|
      if res.id == @resource.id
        if res_index > 0
          @res_prev = res_all[res_index - 1]
        else
          @res_prev = res_all[res_index]
        end
        if res_index < res_all.size - 1
          @res_next = res_all[res_index + 1]
        else
          @res_next = nil
        end

        if res_index < res_all.size - 2
          @res_nnext = res_all[res_index + 2]
        else
          @res_nnext = nil
        end
      end
    end
    
    #go_page为空则为直接点击链接进入
    if @go_page.nil? || @go_page.to_i == 0
      @img_arry = get_resource_pagin(@resource,$larger,@pp_num)
    else
      @img_arry = get_resource_pagin_array(@resource,$larger,@go_page,@pp_num)
    end
    #套图标签
    @tag_arry = ResTag.find_all_by_res_id @resource.id

   
    unless oper_type.nil?
      if oper_type.to_i == 1
        @resource.update_attributes({:support_num => (@resource.support_num.to_i + 1)})
      else
        @resource.update_attributes({:oppose_num => (@resource.oppose_num.to_i + 1)})
      end
      params[:oper_type] = nil
    end

    user_cfg = UserCfg.find_last_by_sid params[:sid]
    if !comment.nil? && !comment.empty?
      user_com = UserComment.find_last_by_sid params[:sid]
      unless user_com.nil?
        if Time.now.to_i - user_com.created_at.to_i >= 10
          ucom = UserComment.new          
          ucom.comment = comment
          ucom.nickname = user_cfg.nickname
          ucom.qq = user_cfg.qq
          ucom.res_id = @resource.id
          ucom.sid = params[:sid]
          ucom.user_id = user_cfg.id
          ucom.user_sign = user_cfg.user_sign
          ucom.user_type = user_cfg.user_type
          ucom.save
        end
      else
        ucom = UserComment.new       
        ucom.comment = comment
        ucom.nickname = user_cfg.nickname
        ucom.qq = user_cfg.qq
        ucom.res_id = @resource.id
        ucom.sid = params[:sid]
        ucom.user_id = user_cfg.id
        ucom.user_sign = user_cfg.user_sign
        ucom.user_type = user_cfg.user_type
        ucom.save
      end
    end

    #设置高清浏览进来的图
    @show_ts = false
    if params[:p_s].to_i == 2
      @show_ts = true      
      #user_cfg.update_attributes({:updated_at => Time.now,:pp_style => params[:p_s]})
      params[:p_s] = nil
    end
    if params[:p_n].to_i !=0
      @show_ts = true
      #user_cfg.update_attributes({:updated_at => Time.now,:pp_num => params[:p_n]})
      params[:p_n] = nil
    end    

    #评论和投票不算点击
    if comment.nil? && oper_type.nil?
      @resource.update_attributes({:click_num => (@resource.click_num.to_i + 1)})      
    end

    #评论
    @user_comments = UserComment.find :all,
      :conditions =>['res_id = ?',@resource.id],
      :order => 'created_at desc',:limit => '2'
    @uc_count =  UserComment.count(:all, :conditions => "res_id = #{@resource.id}")
    @titles = @resource.name
    #提示注册几率
    rs = rand_get(8,1)
    flag = false
    rs.each do |i|
      if i.to_i == 0 && params[:cn].to_i > 8
        flag = true
      end
    end
    
    begin
      if flag && get_user_type.to_i != 2 && params[:is_a].to_i != 1 && params[:f_i].to_i != 1
        redirect_to params.merge!(:action => 'ts')
      else
        params[:f_i] = nil
        respond_to do |format|
          format.wml
        end
      end
    rescue      
    end
  end

  #极速小图,版面位置比较多
  def show_small
    if get_user_type.to_i != 2 && params[:c_t].to_i == 1      
      params[:c_t] = nil
      redirect_to params.merge!(:action => 'setup')
    end
    params[:cn] = params[:cn].to_i + 1
    @oper_type = 5
    @resource = Resource.find(params[:resource])
    #鲜花和鸡蛋
    oper_type = params[:oper_type]
    #评论 时间间隔要大于10秒
    comment = params[:comment]
    res_score = 0.0
    if oper_type.nil? && !params[:page].nil?
      #翻页过来的
      res_score = 1.5      
    elsif oper_type.nil? && params[:page].nil?
      #栏目过来的
      res_score = 1.0
    elsif !oper_type.nil? && oper_type.to_i == 1
      #鲜花过来的
      res_score = 2.0
    end   
    d_s = @resource.day_score + res_score.to_f
    w_s = @resource.week_score + res_score.to_f
    m_s = @resource.month_score + res_score.to_f
    @resource.update_attributes({:score => (@resource.score.to_f + res_score.to_f).to_f,
        :day_score => d_s,:week_score => w_s,:month_score => m_s})
    if not params[:jump_page].nil?
      if params[:jump_page].to_i == 0
        params[:jump_page] = 1
      end
      if params[:jump_page].to_i * $img_per_page > params[:total_pages].to_i
        params[:jump_page] = params[:total_pages].to_i * $img_per_page
      end
      if params[:jump_page].to_i % $img_per_page == 0
        params[:page] = (params[:jump_page].to_i / $img_per_page).to_i
      else
        params[:page] = (params[:jump_page].to_i / $img_per_page).to_i + 1
      end
      params[:id] = nil
      params[:jump_page] = nil
    end
   
    @back_type = $back_col
    if params[:return_type].to_i == 1
      @back_type = $back_lastest
    elsif params[:return_type].to_i == 3
      @back_type = $back_chose
    elsif params[:return_type].to_i == 4
      @back_type = $back_al
    elsif params[:return_type].to_i == 9
      @back_type = $back_rank
    end
    @rt = params[:rt]
    section = Section.find @resource.col_id
    @col = section
    @page_for = params[:page_for]
    unless params[:page].nil?
      @go_page = params[:page]
    else
      @go_page = params[:go_page]
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    host = request.domain
    le = $level_map[host.swapcase]
    res_l = le[:res_level]
    @titles = le[:name]
    res_all = Resource.find :all,
      :conditions =>  ["status=? and col_id=?  and level <= ?", 1, @resource.col_id,res_l],
      :order => 'orders asc'   
    res_all.each_with_index do |res,res_index|
      if res.id == @resource.id        
        if res_index > 0
          @res_prev = res_all[res_index - 1]
        else
          @res_prev = res_all[res_index]
        end
        if res_index < res_all.size - 1
          @res_next = res_all[res_index + 1]
        else
          @res_next = nil
        end

        if res_index < res_all.size - 2
          @res_nnext = res_all[res_index + 2]
        else
          @res_nnext = nil
        end
      end
    end
    
    #go_page为空则为直接点击链接进入
    if @go_page.nil? || @go_page.to_i == 0
      @img_arry = get_resource_pagin(@resource,$small,@pp_num)
    else
      @img_arry = get_resource_pagin_array(@resource,$small,@go_page,@pp_num)
    end
    #套图标签
    @tag_arry = ResTag.find_all_by_res_id @resource.id
   
    unless oper_type.nil?
      if oper_type.to_i == 1       
        @resource.update_attributes({:support_num => (@resource.support_num.to_i + 1)})
      else
        @resource.update_attributes({:oppose_num => (@resource.oppose_num.to_i + 1)})
      end
      params[:oper_type] = nil
    end

    user_cfg = UserCfg.find_last_by_sid params[:sid]
    if !comment.nil? && !comment.empty?
      user_com = UserComment.find_last_by_sid params[:sid]
      unless user_com.nil?
        if Time.now.to_i - user_com.created_at.to_i >= 10
          ucom = UserComment.new          
          ucom.comment = comment
          ucom.nickname = user_cfg.nickname
          ucom.qq = user_cfg.qq
          ucom.res_id = @resource.id
          ucom.sid = params[:sid]
          ucom.user_id = user_cfg.id
          ucom.user_sign = user_cfg.user_sign
          ucom.user_type = user_cfg.user_type
          ucom.save
        end
      else
        ucom = UserComment.new        
        ucom.comment = comment
        ucom.nickname = user_cfg.nickname
        ucom.qq = user_cfg.qq
        ucom.res_id = @resource.id
        ucom.sid = params[:sid]
        ucom.user_id = user_cfg.id
        ucom.user_sign = user_cfg.user_sign
        ucom.user_type = user_cfg.user_type
        ucom.save
      end
    end

    #设置高清浏览进来的图
    @show_ts = false
    if params[:p_s].to_i == 1
      @show_ts = true      
      #user_cfg.update_attributes({:updated_at => Time.now,:pp_style => params[:p_s]})
      params[:p_s] = nil
    end
    if params[:p_n].to_i !=0
      @show_ts = true
      #user_cfg.update_attributes({:updated_at => Time.now,:pp_num => params[:p_n]})
      params[:p_n] = nil
    end

    #评论和投票不算点击
    if comment.nil? && oper_type.nil?
      @resource.update_attributes({:click_num => (@resource.click_num.to_i + 1)})      
    end

    #评论
    @user_comments = UserComment.find :all,
      :conditions =>['res_id = ?',@resource.id],
      :order => 'created_at desc',:limit => '2'
    @uc_count =  UserComment.count(:all, :conditions => "res_id = #{@resource.id}")
    @titles = @resource.name
    #提示注册几率
    rs = rand_get(8,1)
    flag = false
    rs.each do |i|
      if i.to_i == 0 && params[:cn].to_i > 8
        flag = true
      end
    end
    begin
      if flag && get_user_type.to_i != 2 && params[:is_a].to_i != 1 && params[:f_i].to_i != 1
        redirect_to params.merge!(:action => 'ts')
      else
        params[:f_i] = nil
        respond_to do |format|
          format.wml
        end
      end
    rescue
    end
  end

  #标签进入
  def series
    params[:cn] = params[:cn].to_i + 1
    @oper_type = 6
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    if not params[:jump_page].nil?
      if params[:jump_page].to_i == 0
        params[:jump_page] = 1
      end
      if params[:jump_page].to_i <= params[:total_pages].to_i
        params[:page] = params[:jump_page].to_i
      else
        params[:page] = params[:total_pages]
      end
      if params[:page].to_i > params[:total_pages].to_i
        params[:page] = params[:total_pages]
      end
      params[:jump_page] = nil
    end
    @go_page = 1
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end

    unless params[:page_for].nil?
      to_page = params[:page_for]
      params[:page_for] = nil
    end
    @page_for = params[:page]

    host = request.domain
    le = $level_map[host.swapcase]    
    res_l = le[:res_level]
    per_page = $res_per_page_word
    if @img_style == 2.to_s
      per_page = $res_per_page_img
    end
    to_page = params[:page]
    if to_page.nil?
      to_page = 1
    end    
    begin
      @tg = Tag.find(params[:tag])
      #标签点击次数
      unless @tg.nil?
        if params[:type].nil?
          @tg.update_attributes({:click_num => (@tg.click_num + 1)})
        end
      end
    rescue
      @tg =Tag.find_last_by_level res_l
    end
    @rts = ResTag.paginate :per_page => per_page, :page =>  to_page,
      :conditions  =>  ["tag_id = ? and resources.status=? and resources.level <= ? ", @tg.id,1,res_l],
      :include => [:resource]
    @titles = "["+@tg.name+"]搜索"
    @search = @tg.name
    respond_to do |format|
      format.wml
    end
  end

  def error
    params[:cn] = params[:cn].to_i + 1
    @message = params[:message]
    respond_to do |format|
      format.wml
    end
  end

  #广告的转发,统计
  def ad
    params[:cn] = params[:cn].to_i + 1
    @oper_type = 7
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    section = Section.find(params[:id])
    if section.ad_url.index('?').nil?
      url = section.ad_url + "?sid="+request.params[:sid]
    else
      url = section.ad_url + "&sid="+request.params[:sid]
    end
    redirect_to url
  end

  #3个位置的广告转发和统计
  def show_more
    ad = Ad.find(params[:ad])
    unless ad.nil?
      ad_s = AdStatic.new
      ad_s.ad_id = ad.id
      ad_s.ad_name = ad.name
      ad_s.orders = ad.orders
      ad_s.save
      redirect_to ad.url
    end
  end

  def download
    params[:cn] = params[:cn].to_i + 1
    @oper_type = 9
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:page]
    unless params[:page].nil?
      @go_page = params[:page]
    else
      @go_page = params[:go_page]
    end
    @back_type = nil
    @resource = Resource.find(params[:resource])
    section = Section.find @resource.col_id
    @col = section
    @view_img_path = get_resource_pagin_to_download(@resource,params[:type],params[:index].to_i,@pp_num,@go_page)
    small = @view_img_path
    large = @view_img_path
    src = @view_img_path
    @small_down_path = small.gsub(params[:type], $small)
    @large_down_path = large.gsub(params[:type], $larger)
    @src_down_path =  src.gsub(params[:type], $src)
    @small_size = format_filesize @small_down_path
    @large_size = format_filesize @large_down_path
    @src_size = format_filesize @src_down_path

    host = request.domain
    le = $level_map[host.swapcase]
    @titles = "免费下载" +request.domain

    respond_to do |format|
      format.wml
    end
  end

  def hot
    params[:cn] = params[:cn].to_i + 1
    @titles = "最新"
    @type = params[:return_id].to_s
    if @type == '1'
      get_res
      @titles = "最新"
    elsif @type =='2'
      get_res_cat
      @back_type = nil
      @titles = "分类"
    else
      get_res
      @titles = "最新"
    end
    respond_to do |format|
      format.wml
    end
  end

  def search
    params[:cn] = params[:cn].to_i + 1
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    if not params[:jump_pages].nil?
      if params[:jump_pages].to_i == 0
        params[:jump_pages] = 1
      end
      if params[:jump_pages].to_i <= params[:total_pages].to_i
        params[:page] = params[:jump_pages].to_i
      else
        params[:page] = params[:total_pages]
      end
      if params[:page].to_i > params[:total_pages].to_i
        params[:page] = params[:total_pages]
      end
      params[:jump_page] = nil
    end
    @back_type = nil
    @go_page = 1
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    to_page = params[:page]
    unless params[:page_for].nil?
      @page_for = params[:page]
      to_page = @page_for
      params[:page_for] = nil
    end
    if to_page.nil? || to_page.to_i == 0
      to_page = 1
    end
    host = request.domain
    le = $level_map[host.swapcase]
    @titles = le[:name]
    res_l = le[:res_level]
    per_page = $res_per_page_word
    if @img_style == 2.to_s
      per_page = $res_per_page_img
    end   
    @search = params[:search]
    if @search.nil?
      @search = '美女'
    end
    #    puts "@pp_num ------------ #{@pp_num}"
    @resouces = Resource.paginate :per_page => per_page, :page =>  to_page,
      :conditions =>  ["status=? and name like ?  and level <= ?", 1, '%'+@search+'%',res_l],
      :order => 'created_at desc'
    @titles = "站内搜索"
    respond_to do |format|
      format.wml
    end
  end

  def search_more
    params[:cn] = params[:cn].to_i + 1
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    @back_type = nil   
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @titles = "更多搜索"
    respond_to do |format|
      format.wml
    end
  end

  #点击排行
  def rank
    params[:cn] = params[:cn].to_i + 1
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end   
    if not params[:jump_page].nil?
      if params[:jump_page].to_i == 0
        params[:jump_page] = 1
      end
      if params[:jump_page].to_i <= params[:total_pages].to_i
        params[:page] = params[:jump_page].to_i
        
      else
        params[:page] = params[:total_pages]
      end
      if params[:page].to_i > params[:total_pages].to_i
        params[:page] = params[:total_pages]
      end
      params[:jump_page] = nil
    end
    @back_type = nil
    @go_page = 1
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end    
    to_page = params[:page]
    unless params[:page_for].nil?
      @page_for = params[:page]
      to_page = @page_for
      params[:page_for] = nil
    end
    if to_page.nil? || to_page.to_i == 0
      to_page = 1
    end
    host = request.domain
    le = $level_map[host.swapcase]
    @titles = le[:name]
    res_l = le[:res_level]
    per_page = $res_per_page_word
    if @img_style == 2.to_s
      per_page = $res_per_page_img
    end

    @rt = params[:rt].to_i
    if params[:rt].to_i == 0
      @rt = 1
    end

    if params[:rt].to_i == 1
      t_l ="总榜"
      res = Resource.find :all,
        :order => 'score desc',
        :limit => '45'
    elsif params[:rt].to_i == 2
      t_l ="月榜"
      res = Resource.find :all,
        :order => 'month_score desc',
        :limit => '45'
    elsif params[:rt].to_i == 3
      t_l ="周榜"
      res = Resource.find :all,
        :order => 'week_score desc',
        :limit => '45'
    elsif params[:rt].to_i == 4
      t_l ="日榜"
      res = Resource.find :all,
        :order => 'day_score desc',
        :limit => '45'
    else
      t_l ="总榜"
      res = Resource.find :all,
        :order => 'score desc',
        :limit => '45'
    end
    
    @res_array = res.paginate(:page =>to_page,:per_page =>per_page)
    @titles = "热辣排行-#{t_l}"
    respond_to do |format|
      format.wml
    end
  end
  
  #分类
  def cat
    params[:cn] = params[:cn].to_i + 1
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    host = request.domain
    le = $level_map[host.swapcase]
    @channels = Channel.find :all , :conditions => ['status = 1 and is_cat = ?', 1], :order => 'orders asc'
    @titles = "[分类]" + "搜索"
    respond_to do |format|
      format.wml
    end
  end

  #更多标签
  def more_series
    params[:cn] = params[:cn].to_i + 1
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    @back_type = nil
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    res_tags = ResTag.find_by_sql("select count(*) as ac,tag_id from res_tags group by tag_id order by ac desc limit 20")
    str = ""
    res_tags.each do |rt|      
      str = rt.tag_id.to_s+","+str
    end    
    str = str[0,(str.length - 1)]    
    @m_series = Tag.find_by_sql("select * from tags where id in (#{str})")
    @titles = "更多标签"
    respond_to do |format|
      format.wml
    end
  end

  #显示专辑栏目封面
  def show_al
    params[:cn] = params[:cn].to_i + 1
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    @back_type = nil
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @section = Section.find(params[:id])
    @titles = @section.name
    @back_type = $back_al
    #专辑频道
    @channels_al = Channel.find :all , :conditions => ['status = 1 and channel_type = 2'], :order => 'orders asc'
    @channel_al = nil
    if @channels_al.size > 0
      @channel_al = @channels_al[0]
    end
    respond_to do |format|
      format.wml
    end
  end

  #专辑频道下的栏目展示
  def channel_al
    params[:cn] = params[:cn].to_i + 1
    @oper_type = 17
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    if not params[:jump_page].nil?
      if params[:jump_page].to_i == 0
        params[:jump_page] = 1
      end
      if params[:jump_page].to_i <= params[:total_pages].to_i
        params[:page] = params[:jump_page].to_i
      else
        params[:page] = params[:total_pages]
      end
      if params[:page].to_i > params[:total_pages].to_i
        params[:page] = params[:total_pages]
      end
      params[:jump_page] = nil
    end
    @go_page = 1
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end

    unless params[:page_for].nil?
      to_page = params[:page_for]
      params[:page_for] = nil
    end
    @page_for = params[:page]

    host = request.domain
    le = $level_map[host.swapcase]
    @titles = le[:name]
    res_l = le[:res_level]
    per_page = $res_per_page_word
    if @img_style == 2.to_s
      per_page = $res_per_page_word
    end
    to_page = params[:page]
    if to_page.nil?
      to_page = 1
    end

    channel = Channel.find(params[:id])
    @sections = Section.paginate :per_page => per_page, :page =>  to_page,
      :conditions =>  ["status=? and channel_id = ?", 1,channel.id],
      :order => 'created_at desc'
    @titles = channel.name
    respond_to do |format|
      format.wml
    end
  end

  #精选频道显示所有栏目
  def channel_chose
    params[:cn] = params[:cn].to_i + 1
    @oper_type = 18
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    if not params[:jump_page].nil?
      if params[:jump_page].to_i == 0
        params[:jump_page] = 1
      end
      if params[:jump_page].to_i <= params[:total_pages].to_i
        params[:page] = params[:jump_page].to_i
      else
        params[:page] = params[:total_pages]
      end
      if params[:page].to_i > params[:total_pages].to_i
        params[:page] = params[:total_pages]
      end
      params[:jump_page] = nil
    end   
    @go_page = 1
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end

    unless params[:page_for].nil?
      to_page = params[:page_for]
      params[:page_for] = nil
    end
    @page_for = params[:page]

    host = request.domain
    le = $level_map[host.swapcase]
    @titles = le[:name]
    res_l = le[:res_level]
    per_page = $res_per_page_word
    if @img_style == 2.to_s
      per_page = $res_per_page_img
    end
    to_page = params[:page]
    if to_page.nil?
      to_page = 1
    end

    channel_id = params[:id]
    if channel_id.nil? || channel_id.to_i ==0
      channel = Channel.find_last_by_channel_type "3"
      channel_id = channel.id
    end
    @resources = Resource.paginate :per_page => per_page, :page =>  to_page,
      :conditions =>  ["status=? and channel_id=?", 1,channel_id],
      :order => 'score desc'
    @titles = "精选套图推荐"
    respond_to do |format|
      format.wml
    end
  end

  def test
    @titles = "测试格式错误"
    @resource = Resource.find(1)
    @img_arry = get_resource_pagin_array(@resource,$larger,@go_page,@pp_num)
    respond_to do |format|
      format.wml
    end
  end

  #随机频道
  def rands
    params[:cn] = params[:cn].to_i + 1
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    @back_type = nil
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    host = request.domain
    le = $level_map[host.swapcase]
    col_l = le[:col_level]
    @resouces = Resource.find :all,
      :conditions =>  ["status=?  and level <= ?", 1,col_l],
      :order => 'updated_at desc',
      :limit => '15'
    @titles = "随机美图"
    respond_to do |format|
      format.wml
    end
  end

  #更多广告
  def more_ads
    params[:cn] = params[:cn].to_i
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    @back_type = nil
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    host = request.domain
    le = $level_map[host.swapcase]
    @ads = Ad.find :all,
      :conditions =>['status = ? and orders = ?','1',4],
      :order => 'x,y'    
    @titles = "酷炫好玩地带"
    respond_to do |format|
      format.wml
    end
  end

  def ps str
    puts "------------------------------------"
    puts str
    puts "------------------------------------"
  end
end
